<?php
namespace app\api\controller;
use app\BaseController;
use think\facade\Db;
class Goods extends Base{
    //后台获取 产品 数据列表
    public function get_list_data_a(){
        $param = input('');

        $search_type = $param['type_id'] ? ['type_id' => $param['type_id']]:[];//判断是否需要筛选 分类
        $search = $param['keyWord'] ? [['productName', 'like', '%'.$param['keyWord'].'%']]:[];//判断是否需要筛选 关键词
        $search_sort = $param['sortField'] ? [$param['sortField'] => $param['sort']]:[];//排序
        $list = Db::table('an_goods')
        ->order($search_sort)
        ->page($param['page'],$param['limit'])
        ->where($search_type)
        ->where($search)
        ->withoutField(['content'])
        ->select();

        $this->info['data'] = [
            'list' => $list,
            'type' => Db::table('an_type')->where('type', 'goods')->order('id', 'desc')->select()->toArray()
        ];
        $this->info['total'] = Db::table('an_goods')->where($search_type)->where($search)->count();
        return json($this->info);
    }    

    //获取需要编辑数据
    public function get_detail_a(){
        $param = input('');
        $details = null;
        if($param['id']){
            $details = Db::table('an_goods')->where('id',$param['id'])->find();
        }
        $this->info['data'] = [
            'details'=>$details,
            'type' => Db::table('an_type')->where('type', 'goods')->order('id', 'desc')->select()->toArray()
        ];
        return json($this->info);
    }

    //删除数据
    public function del_data_a(){
        $param = input('');
        foreach($param['ids'] as $v){
            Db::table('an_goods')->where('id',$v)->delete();
        }
        $this->logData('删除产品，id为'.implode(",", $param['ids']));
        return json($this->info);
    }

    //保存数据 
    public function saveData(){
        $param = input('');
        $data = [
            'up_time'=>time(),
            'productName'=>$param['productName'],
            'productImage'=>$param['productImage'],
            'productPrice'=>isset($param['productPrice'])?$param['productPrice']:0,
            'productStock'=>isset($param['productStock'])?$param['productStock']:0,
            'content'=>$param['content'],
            'type_id'=>$param['type_id'],
            "a_show"=>trim($param['a_show']),
            'sort'=>isset($param['sort'])?$param['sort']:0,
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'description'=>isset($param['description'])?$param['description']:'',
        ];
        if(empty($param['id'])){
            $data['create_time'] = time();
            $this->info['msg']='新增产品成功';
        }else{
            $data['id'] = $param['id'];
            $this->info['msg']='修改产品成功';
        }
        $this->logData($this->info['msg'].'，名称为'.$param['productName']);

        //save 自动判断是编辑更新数据 还是 插入新数据
        $this->info['data'] = Db::table('an_goods')->save($data);
        return json($this->info);
    }  

    //产品批量上下架
    public function switch_state_data_a(){
        $param = input('');
        foreach($param['ids'] as $v){
            Db::table('an_goods')->where('id',$v)->update(['down_goods'=>$param['state']]);
        }
        $str = $param['state']==1?'下架':'上架';
        $this->logData($str.'产品，id为'.implode(",", $param['ids']));
        return json($this->info);
    }

}
